// JavaScript Document
var wait ;
var rule_tel = /^13[0-9]{1}[0-9]{8}|^18[0-9]{1}[0-9]{8}|^15[0-9]{1}[0-9]{8}$/;
var rule_pwd = /^^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,15}$/;
var length_pwd = /^.{6,15}$/;
var otel = $("#register-tel"),
		opsd = $("#register-psd"),
		oyzm = $("#yzm-tel");
var msgError = $('#msg-error');
var value1 ='',value2='',value3='';
//注册校验
$('#register-user').on('click',function(){
	var tel = $("#register-tel").val();
	var pwd = $("#register-psd").val();
	var yzm = $("#yzm-tel").val();
	var flag = true;
	if(tel == ""){
		value1 = '请输入手机号!';
		otel.addClass('val-error');		 
		//alert("请输入手机号!");
		flag = false;
	}else if(!rule_tel.test(tel)){
		value1 = '请输入正确的手机号!';
		otel.addClass('val-error');
		//alert("请输入正确的手机号!");
		flag = false;
	}else if(yzm == null || yzm == "" ){
		oyzm.addClass('val-error');
		value3 = '请输入验证码!';
		//alert("请输入验证码!");
		flag = false;
	}else if(pwd == null || pwd == "" ){
		opsd.addClass('val-error');
		value2 = '请输入密码!';
		//alert("请输入密码!");
		flag = false;
	}else if(!length_pwd.test(pwd)){
		psd.addClass('val-error');
		value2 = '请输入正确的密码长度!';
		//alert("请输入正确的密码长度!");
		flag = false;
	}else if(!rule_pwd.test(pwd)){
		psd.addClass('val-error');
		value2 = '请输入正确的密码格式!';
		//alert("请输入正确的密码格式!");
		flag = false;
	}
	if(rule_tel.test(tel)){
		otel.removeClass('val-error').addClass('val-success');
		value1='';
	}
	if(length_pwd.test(pwd)){
		opsd.removeClass('val-error').addClass('val-success');
		value2='';
	}
	msgError.text('');
	 msgError.show().text(value1+value2+value3);
	if(flag){
		msgError.hide();
	 	otel.removeClass('val-error').addClass('val-success');
	 	opsd.removeClass('val-error').addClass('val-success');
	 	oyzm.removeClass('val-error').addClass('val-success');
		$.ajax({
	    	type:'post',
	        url:"user/register",
	        data: $("#register-form").serialize(),
	        success: function(result){
	        	if(result.MSG != null){
	        		//alert(result.MSG);
	        		msgError.show().text(result.MSG);
	        		
	        	}else{
	        		location.href="index/home";
	        	}
	         }
			});
	}
});
	

	//获取手机验证码校验
$('#getCode').on('click',function(){
	var yzm_btn = document.getElementById("getCode");
	var tel = $("#register-tel").val();
	if(tel != ""&&rule_tel.test(tel)){
		$.ajax({
	    	type:'get',
	        url:"user/isMobileEngaged?tel="+$("#register-tel").val(),
	        success: function(result){
	        	if(result.STATE == "1"){
	        		wait = 60;
	    			yzm_btn.disabled = false;
	    			time(yzm_btn);
	    			sendCode(result.STATUS,$("#register-tel").val());
	        	}else{
	        		msgError.show().text(result.MSG);
	        		otel.removeClass('val-error val-success').addClass('val-error');
	        		//alert(result.MSG);
	        	}
	         }
			});
	}else {
		otel.removeClass('val-error val-success').addClass('val-error');
		msgError.show().text('请输入正确的手机号!');
		//alert("请输入正确的手机号!");
	}
});
	
	
	//发送手机验证码
	function sendCode(status,tel){
		$.ajax({
	    	type:'get',
	        url:"user/code?tel="+tel+"&status="+status,
	        success: function(result){
	        	if(result.MSG != null && result.MSG != ""){
	        		//alert(result.MSG);
	        		msgError.show().text(result.MSG);
	        		oyzm.removeClass('val-error val-success').addClass('val-error');
	        	}
	         }
			});
	}
	
	
	//倒计时获取验证码
	function time(o) {
		if (wait == 0) {
			o.removeAttribute("disabled");
			o.value = "获取验证码";
			wait = 60;
		} else {
			o.setAttribute("disabled", true);
			o.value = wait + "秒后重新获取";
			wait--;
			setTimeout(function() {
				time(o);
			}, 1000);
		}
	}
